package com.coastline.fruit.common.utils;

import com.coastline.fruit.common.config.ElasticsearchClient;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author Coastline
 * @date 2025/1/29 22:27
 */
@Slf4j
@Component
public class ElasticsearchUtil {

    @Resource
    private ElasticsearchClient client;

    /**
     * 创建索引库
     *
     * @param mappingTemplate
     * @param indexName
     * @return
     * @throws IOException
     */
    public boolean createIndex(String mappingTemplate, String indexName) throws IOException {
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.source(mappingTemplate, XContentType.JSON);
            client.getInstance().indices().create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("创建索引失败", e);
            return false;
        }
        return true;
    }

    // todo 创建指定mapping的索引库的方法

    /**
     * 向指定的Elasticsearch索引添加字段
     *
     * @param indexName 索引名称
     * @param fieldName 字段名称
     * @param fieldType 字段类型
     * @throws IOException 如果操作过程中发生IO异常
     */
    public void addFieldToIndex(String indexName, String fieldName, String fieldType) throws IOException {
        // 定义要添加的字段映射
        String mapping = String.format("{\"properties\": {\"%s\": {\"type\": \"%s\"}}}", fieldName, fieldType);

        // 创建PutMappingRequest实例
        PutMappingRequest request = new PutMappingRequest(indexName);
        request.source(mapping, XContentType.JSON);

        // 执行映射更新请求
        client.getInstance().indices().putMapping(request, RequestOptions.DEFAULT);
    }

    /**
     * 为指定的索引库添加字段
     *
     * @param mapping 字段值mapping
     * @param indexName 索引库
     * @throws IOException 异常
     */
    public void addFieldToIndex(String mapping, String indexName) throws IOException {
        // 创建PutMappingRequest实例
        PutMappingRequest request = new PutMappingRequest(indexName);
        request.source(mapping, XContentType.JSON);

        // 执行映射更新请求
        client.getInstance().indices().putMapping(request, RequestOptions.DEFAULT);
    }

    /**
     * 检查索引库是否存在
     *
     * @param indexName 索引库名称
     * @return
     * @throws IOException
     */
    public boolean checkIndexExists(String indexName) throws IOException {
        try {
            GetIndexRequest request = new GetIndexRequest(indexName);
            return client.getInstance().indices().exists(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("查询索引失败", e);
            return false;
        }
    }

    /**
     * 删除索引库
     *
     * @param indexName
     * @return
     * @throws IOException
     */
    public boolean deleteIndex(String indexName) throws IOException {
        try {
            client.getInstance().indices().delete(new DeleteIndexRequest(indexName), RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("删除索引失败", e);
            return false;
        }
        return true;
    }
}
